package com.project.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.project.service.UserService;
import com.project.util.StringUtil;
import com.project.vo.User;

public class RegisterAction extends ActionSupport implements ModelDriven<User>{

	private UserService userService;
	private User user = new User();
	private Map<String, Object> request;
	private Map<String, Object> session;
	private Map<String, Object> application;
	
	public RegisterAction() {
		ActionContext context = ActionContext.getContext();
		request = (Map<String, Object>) context.get("request");
		session = context.getSession();
		setApplication(context.getApplication());
	}
	
	public String register() {
		System.out.println("stue="+ user.getId()+ "  " +user.getName());
		String result = userService.register(user);
		if (result.equals(StringUtil.SUCCESS)) {
			User newuser = userService.login(user.getId(), user.getPassword());
			if (newuser == null) {
				session.put(StringUtil.LOGINSTATE, StringUtil.LOGOUT);
				return StringUtil.FAILURE;
			}
			session.put(StringUtil.USER, newuser);
			session.put(StringUtil.LOGINSTATE, StringUtil.LOGIN);
		}else if (result.equals(StringUtil.E_DUPLICATENAME)) {
			request.put(StringUtil.RESULT, StringUtil.E_DUPLICATENAME);
			result = StringUtil.FAILURE;
		}else {
			request.put(StringUtil.RESULT, StringUtil.FAILURE);
		}
		return result;
	}
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}

	public Map<String, Object> getApplication() {
		return application;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

}
